import random
import time
import requests
import re
from multiprocessing import Pool

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36'
}

# 获取IP
def findip(base_url):

    response = requests.get(base_url, headers=headers)
    ips = re.findall('<td data-title="IP">(.*?)</td>', response.text)
    ports = re.findall('<td data-title="PORT">(.*?)</td>', response.text)
    for i in range(len(ips)):
        a_ip = ips[i] + ':' + ports[i]
        yield a_ip
        print(a_ip)
    time.sleep(random.randint(1, 3))

# 尝试可用IP
def checkip(ips):

    urllib = ['http://www.baidu.com', 'http://www.huya.com', 'https://www.qidian.com/', 'http:www.bilibili.com']
    url = random.choice(urllib)
    proxies = {
        "http:": "http://" + ips,
        "https:": "https://" + ips
    }
    try:
        response = requests.get(url, headers=headers, proxies=proxies, timeout=0.1)
        if response.status_code == 200:
            return ips
        else:
            print('请求 != 200', ips)
    except:
        print("出现错误，无用的IP", ips)


# if __name__ == '__main__':
#     # 开启进程
#     p = Pool(20)
#     for i in range(1, 10):
#         url = f'https://www.kuaidaili.com/free/inha/{i}'
#         print(f'-------------------第{i}页开始-------------', url)
#         ip_list = findip(url)
#         for item in p.map(checkip, ip_list):
#             if item == None:
#                 pass
#             else:
#                 print('可用的IP' + item)
#                 with open('可用的IP.txt', 'a+', encoding='utf-8')as f:
#                     f.write(item + '\n')

